package com.lqe.mapper;
import com.lqe.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    @Select("select *,id userId from t_user")
    List<User> queryAllUser();

    @Select("select *,id userId from t_user where id = #{id}")
    User queryUserById(int id);

    List<User> queryUserAndRole();

    @Select("select id,username,age from t_user")
    @Results({
            @Result(id = true,column = "id", property = "userId"),
            @Result(column = "username", property = "username"),
            @Result(column = "age", property = "age"),
            @Result(
                    column = "id",
                    property = "orderList",
                    javaType = List.class,
                    many = @Many(select = "com.lqe.mapper.OrderMapper.queryOrderByUid")
            )
    })
    List<User> queryUserAndOrders();

    @Select("select id,username,age from t_user where id =#{id}")
    @Results({
            @Result(id = true,column = "id", property = "userId"),
            @Result(column = "username", property = "username"),
            @Result(column = "age", property = "age"),
            @Result(
                    column = "id",
                    property = "orderList",
                    javaType = List.class,
                    many = @Many(select = "com.lqe.mapper.OrderMapper.queryOrderByUid")
            )
    })
    User queryUserAndOrdersById(int id);

    @Insert("insert into t_user values(null,#{username},#{age})")
    void insertUser(User user);

    @Delete("delete from t_user where id =#{in}")
    void deleteUserById(int id);

    @Update("update t_user set username = #{username},age = #{age} where id = #{userId}")
    void updateUser(User user);

}
